'use strict'
// dependencies
const vkmail = require('vk-mail')

exports.main = async (event, context) => {
    let {user, book} = event
    let emailService = vkmail.createTransport({
        host: 'smtp.163.com',
        port: 465,
        auth: {
            user: 'dtc_book_corner@163.com',
            pass: 'GXZHXTZSSKBIFSYP'
        }
    })
    let admins = (await uniCloud.databaseForJQL({
        event,
        context
    }).collection('user').where('role == 1').field('email').get()).data
    for (let admin of admins) {
        if (admin.email === null || admin.email === '')
            continue
        try {
            await emailService.sendMail({
                from: 'dtc_book_corner@163.com',
                to: admin.email,
                cc: 'dtc_book_corner@163.com',
                subject: '用户图书归还提醒',
                text: `用户${user.name}借阅的图书《${book.name}》已申请归还，请及时处理！`
            })
        } catch (e) {
            console.log(e)
        }
    }
}
